﻿

$(function () {
    $("#addrSelBtn").live("click", function () {
        if ($("#addresslist").is(":hidden")) {
            $("#addresslist").stop().show('fast');
            $("#addrSelBtn").html('－');
        } else {
            $("#addresslist").stop().hide('fast');
            $("#addrSelBtn").html('＋');
        }
    });
    $("#btnScoredikou").live("click", function () {
        if ($("#spnScoreInput").is(":hidden")) {
            $("#spnScoreInput").stop().show('fast');
            $("#pallowUseScore").stop().show('fast');
            $("#btnScoredikou").html('－');
        } else {
            $("#spnScoreInput").stop().hide('fast');
            $("#pallowUseScore").stop().hide('fast');
            $("#btnScoredikou").html('＋');
            $('#txtPaidScoreQty').val('0');
        }
    });

    $("#txtPaidScoreQty").keyup(function () {
        if (!$(this).val()) { $(this).val('0'); }
        if (!$(this).val().match(/^\d+$/)) { $(this).val('0'); }
        if ($(this).val()) {
            if (parseInt($(this).val()) > parseInt($("#spnAllowUseScore").html())) {
                $(this).val($("#spnAllowUseScore").html());
            }
            if (parseInt($(this).val()) > parseInt($('#txtPaidScoreQty').attr('max'))) {
                $(this).val($('#txtPaidScoreQty').attr('max'));
            }
            StaticPayAmt();
        }
    });

    $("#addresslist li").live("click", function () {
        var $self = $(this);
        $self.find(" input").attr("checked", true);
    });
    $("#addresslist li").live("click", function () {
        var _self = $(this);
        _self.find(" input").attr("checked", true);
        $('#txtRecAddrId').val(_self.attr("rec-AddrId"));
        $('#spnRecAddrSel1').html(_self.find(".addr-name").html());
        $('#spnRecAddrSel2').html(_self.find(".addr-full").html());
        updateaddress(_self.attr("rec-provId"));
    });
    $("#btnSaveOrder").live("click", function () {
        doSaveOrder();
    });

    $("#dplPROVINCE_ID").change(function () {
        var _jsParam = new jsonRow();
        _jsParam.AddCell("ACTION", "dplPROVINCE_ID_change");
        _jsParam.AddCell("PROVINCE_ID", $(this).val());
        ajaxComm(_jsParam);
    });
    $("#dplCITY_ID").change(function () {
        var _jsParam = new jsonRow();
        _jsParam.AddCell("ACTION", "dplCITY_ID_change");
        _jsParam.AddCell("CITY_ID", $(this).val());
        ajaxComm(_jsParam);
    });
    $("#btnSaveAddr").live("click", function () {
        if (getAllValiInp()) {
            var _jsParam = new jsonRow();
            var _jsPost = new jsonRow();
            GetFormJson("divnewAddrAdd", _jsPost);
            _jsParam.AddCell("ACTION", "doSaveAddr");
            _jsPost.AddCell("ZONE_DESC", $("#dplPROVINCE_ID").find("option:selected").text() + " " + $("#dplCITY_ID").find("option:selected").text() + " " + $("#dplAREA_ID").find("option:selected").text());
            ajaxComm(_jsParam, _jsPost);
        }
    });
});
var issetsetAllValiInp = false;
function showaddnewaddr() {
    $('#divnewAddrAdd').toggle('fast');
    if (!issetsetAllValiInp) {
        issetsetAllValiInp = true;
        setAllValiInp();
    }
};

function StaticPayAmt() {
    var iscoreamt = parseInt($('#txtPaidScoreQty').val()) * parseFloat($('#txtPaidScoreQty').attr('score-price'));
    $("#scoredikouAmt").html(iscoreamt.toFixed(2));
    $("#tatoldikouAmt").html(parseFloat($("#scoredikouAmt").html()).toFixed(2));
    $("#J_ActualFee").html((parseFloat($("#spnPAYABLE_AMT").html()) - parseFloat($("#tatoldikouAmt").html())).toFixed(2));
};
function updateaddress(sId) {
    var pidtm1 = sId || $("#addresslist").find("input:radio[name='address']:checked").parent().attr("rec-provId");
    if ($("#txtPlaceId").val() == pidtm1) {
        return;
    }
    $("#txtPlaceId").val(pidtm1);
    var _jsParam = new jsonRow();
    _jsParam.AddCell("ACTION", "updateaddress");
    _jsParam.AddCell("txtGoodsId", $("#txtGoodsId").val());
    _jsParam.AddCell("txtCartId", $("#txtCartId").val());
    _jsParam.AddCell("txtSrcT", $("#txtSrcT").val());
    _jsParam.AddCell("txtPubNId1", $("#txtPubNId1").val());
    _jsParam.AddCell("txtPubNId2", $("#txtPubNId2").val());
    _jsParam.AddCell("txtBuyQty", $("#txtBuyQty").val());
    _jsParam.AddCell("txtPlaceId", $("#txtPlaceId").val());
    _jsParam.AddCell("RECE_ADDRESS_ID", $('#txtRecAddrId').val()); 
    ajaxComm(_jsParam);
};

function setLogisAmt(sDealId, dLogisamt, dDealAmt) {
    var $orderlisttem1 = $(".cust-order[shop-id='" + sDealId + "']");
    if ($orderlisttem1.length > 0) {
        $orderlisttem1.find(".shop-logisamt").html(dLogisamt);
        $orderlisttem1.find(".shop-totallamt").html((parseFloat(dDealAmt) + parseFloat(dLogisamt)).toFixed(2));
    }
};
function doSaveOrder() {
    if ($('#txtRecAddrId').val()=='') {
        alert("请选择收货地址！");
        return;
    }
    var $orderlisttem1 = $(".cust-order");
    if ($orderlisttem1.length <= 0) {
        alert("当前没有选择商品！");
        return;
    } 
    var loadi = layer.load('正在保存中…');
    try {
        function closeload() { layer.close(loadi); }
        var _jsParam = new jsonRow();
        _jsParam.AddCell("ACTION", "doSaveOrder");
        _jsParam.AddCell("txtGoodsId", $("#txtGoodsId").val());
        _jsParam.AddCell("txtCartId", $("#txtCartId").val());
        _jsParam.AddCell("txtSrcT", $("#txtSrcT").val());
        _jsParam.AddCell("txtPubNId1", $("#txtPubNId1").val());
        _jsParam.AddCell("txtPubNId2", $("#txtPubNId2").val());
        _jsParam.AddCell("txtBuyQty", $("#txtBuyQty").val());
        _jsParam.AddCell("txtPlaceId", $("#txtPlaceId").val());
        _jsParam.AddCell("txtPaidScoreQty", $("#txtPaidScoreQty").val());
        _jsParam.AddCell("RECE_ADDRESS_ID", $('#txtRecAddrId').val()); 
        //        var _jsPost = new jsonRow();
        //        _jsPost.AddCell("RECE_ADDRESS_ID", $addrtem1.val());
        //        _jsPost.AddCell("ORDER_COUNT", $orderlisttem1.length);
        $orderlisttem1.each(function (i, _item) {
            var _self = $(this);
            _jsParam.AddCell("BUY_MEMO_" + _self.attr("shop-id"), _self.find(' textarea').val());
        });

        ajaxComm(_jsParam, null, closeload);
    }
    catch (err) {
        layer.close(loadi);
    }
};


















//function doloadGoodsList() {
//    var $addrtem1 = $("#address-list").find("input:radio[name='address']:checked");
//    if ($addrtem1.length <= 0) {
//        alert("请选择收货地址！");
//        return;
//    }
//    $("#address-list input").attr("disabled", true);
//    $("#J_NewAddressBtn").removeClass("disabled").addClass("disabled");
//    $("#btnConfirmAddr").hide();
//    $("#btnReSelAddr").show();

//    $("#txtPlaceId").val($addrtem1.attr("place-id"));
//    var _jsParam = new jsonRow();
//    _jsParam.AddCell("ACTION", "doloadGoodsList");
//    _jsParam.AddCell("txtGoodsId", $("#txtGoodsId").val());
//    _jsParam.AddCell("txtCartId", $("#txtCartId").val());
//    _jsParam.AddCell("txtSrcT", $("#txtSrcT").val());
//    _jsParam.AddCell("txtPubNId1", $("#txtPubNId1").val());
//    _jsParam.AddCell("txtPubNId2", $("#txtPubNId2").val());
//    _jsParam.AddCell("txtBuyQty", $("#txtBuyQty").val());
//    _jsParam.AddCell("txtPlaceId", $("#txtPlaceId").val());
//    _jsParam.AddCell("RECE_ADDRESS_ID", $('#txtRecAddrId').val());
//    ajaxComm(_jsParam);
//};






















//function StaticPayAmt() {
//    var iscoreamt = parseInt($('#txtPaidScoreQty').val()) * parseFloat($('#txtPaidScoreQty').attr('score-price'));
//    $("#scoredikouAmt").html(iscoreamt.toFixed(2));
//    $("#tatoldikouAmt").html(parseFloat($("#scoredikouAmt").html()).toFixed(2));
//    $("#J_ActualFee").html((parseFloat($("#spnPAYABLE_AMT").html()) - parseFloat($("#tatoldikouAmt").html())).toFixed(2));
//};

//function updateTableHtml(_data) {
//    jQuery.loadHtml('/Template/ConfirmOrderList.html').done(function (_rethtml) {
//        var tpl = HtmlUtil.htmlDecode(_rethtml);
//        laytpl(tpl).render(_data, function (render) {
//            $("#J_OrderList").empty().html(render);
//        });
//    });
//};
//function updateorderaddr() {
//    var $tem321 = $("#address-list .selected");
//    if ($tem321.length > 0) {
//        $("#sendtozone").html($tem321.attr("dr-full"));
//        $("#sendtoreceipt").html($tem321.attr("dr-receipt"));
//    }
//}; 